<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
 * PHP version 5
 *
 * This source file is subject to version 2.1 of the GNU Lesser General Public
 * License, that is bundled with this package in the file COPYING, available
 * through the world wide web at the following URI:
 * http://www.gnu.org/copyleft/lesser.html.
 *
 * @package    ZNF
 * @subpackage Presentation
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: Smarty.php 43 2007-06-26 23:36:35Z aronnax $
 * @since      Release 0.5.0
 * @link       http://www.zeronotice.org
 */

require_once('Smarty/Smarty.class.php');
require_once('ZNF/Presentation/Render.php');

/**
 * <i>ZNF_Presentation_Smarty</i> class extends the <i>ZNF_Presentation_Render</i> class and use the <i>Smarty</i> class of the <i>Smarty</i> package for the renderization.
 *
 * Render data using package or theme templates.
 *
 * @access     public
 * @package    ZNF
 * @subpackage Presentation
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: Smarty.php 43 2007-06-26 23:36:35Z aronnax $
 * @since      Release 0.5.0
 * @link       http://www.zeronotice.org
 */
class ZNF_Presentation_Smarty extends ZNF_Presentation_Render
{

    const SMARTY_CACHING = false;

    const SMARTY_COMPILE_CHECK = true;

    const SMARTY_DEBUGGING = ZNF::DEBUG;

    const SMARTY_FORCE_COMPILE = false;

    /**
     * The <i>Smarty</i> object
     *
     * @access protected
     * @var string $_smarty
     */
    protected $_smarty = null;

    /**
     * Constructs a new <i>ZNF_Presentation_Smarty</i> object and sets the internal properties with application settings.
     *
     * @access public
     * @param string $package Name of the package to which the smarty template
     *                        refers, leave null this parameter to refer to the
     *                        theme smarty templates
     * @param ZNF_Action_ActionMapping $mapping
     */
    public function __construct($package = null, $mapping = null)
    {
        parent::__construct($package, $mapping);

        $this->_smarty = new Smarty();

        $this->_smarty->template_dir = '.';
        $this->_smarty->compile_dir = ZNF::CACHE_DIR;
        $this->_smarty->config_dir = ZNF::CONFIG_DIR;
        $this->_smarty->cache_dir = ZNF::CACHE_DIR;
        $this->_smarty->caching = self::SMARTY_CACHING;
        $this->_smarty->compile_check = self::SMARTY_COMPILE_CHECK;
        $this->_smarty->debugging = self::SMARTY_DEBUGGING;
        $this->_smarty->force_compile = self::SMARTY_FORCE_COMPILE;
    }

    /**
     * Executes and returns the smarty template results.
     *
     * Gets the path of the smarty template, calls
     * <i>ZNF_Presentation_Smarty->_assignConstants()</i> and calls the
     * <i>fetch()</i> method of <i>Smarty</i>.
     *
     * @access public
     * @param string $template File name of the smarty template
     * @param mixed $data Additional data to process
     * @return string
     */
    public function fetch($template, $data = null)
    {
        $template = $this->_getTemplatePath($template, $data);

        $this->_assignConstants();

        return $this->_smarty->fetch($template, null, null, false);
    }

    /**
     * Executes and displays the smarty template results.
     *
     * Gets the path of the smarty template, calls
     * <i>ZNF_Presentation_Smarty->_assignConstants()</i> and calls the
     * <i>display()</i> method of <i>Smarty</i>.
     *
     * @access public
     * @param string $template File name of the smarty template
     * @param mixed $data Additional data to process
     */
    public function display($template, $data = null)
    {
        $template = $this->_getTemplatePath($template, $data);

        $this->_assignConstants();

        $this->_smarty->fetch($template, null, null, true);
    }

    /**
     * Assigns values to smarty template variables.
     *
     * @param string $param The smarty template parameter name
     * @param mixed $value The value to assign
     */
    public function assign($param, $value)
    {
        $this->_smarty->assign($param, $value);
    }

    /**
     * Assign to a smarty template the ZNF constants.
     *
     * @access protected
     */
    protected function _assignConstants()
    {
        $constants = $this->_getConstants();
        $this->_smarty->assign('znf', $constants);
    }

    /**
     * Destroys the <i>ZNF_Presentation_Smarty</i> object.</>
     *
     * @access public
     */
    public function __destruct()
    {
    }

}

/**
 * <i>ZNF_Presentation_SmartyException</i> is the exception type for the <i>ZNF_Presentation_Smarty</i> class.
 *
 * <i>ZNF_Presentation_SmartyException</i> extends the <i>Exception</i> class of PHP5.
 *
 * @access     public
 * @package    ZNF
 * @subpackage Presentation
 * @author     Alessandro Rossini <http://www.alessandrorossini.org>
 * @author     Graziano Liberati <http://www.liberati.org>
 * @copyright  2004-2007 The ZNF Development Team
 * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
 * @version    SVN $Id: Smarty.php 43 2007-06-26 23:36:35Z aronnax $
 * @since      Release 0.5.0
 * @link       http://www.zeronotice.org
 */
class ZNF_Presentation_SmartyException extends Exception
{
}

?>
